package com.yunkaweilai.android.view.dialog.operation;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.lazylibrary.util.PreferencesUtils;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.constants.CommonConstants;
import com.yunkaweilai.android.interfaces.JiciConsumeDialogInterfaces;
import com.yunkaweilai.android.model.SendModel;
import com.yunkaweilai.android.model.member.MemberHeadModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;

import java.util.Calendar;
import java.util.TimeZone;

import cn.addapp.pickers.picker.DatePicker;

/**
 * 会员扣次
 * Created by Administrator on 2016/12/22 0022.
 */

public class OperationJiciReduceDialog extends Dialog {
    ImageView idImgClose;
    private TextView idTvContent;
    private TextView idTvClose;
    private TextView idTvRight;
    private EditText editText;
    private View view;
    private MemberHeadModel model;
    private String other;
    private String content;
    private TextView idTvType;
    private JiciConsumeDialogInterfaces dialogPayInterface;
    private CheckBox idCheckNext, idCheckPrint, idCheckVx, idCheckSms;
    private Activity mactivity;
    private boolean sms = false;
    private boolean wechat = false;
    private boolean print = false;
    private Context context;
    private boolean is_pass = false;

    public OperationJiciReduceDialog(Activity context, String content, JiciConsumeDialogInterfaces dialogPayInterface, boolean sms, boolean wechat, boolean print, boolean is_password) {
        super(context, R.style.dialog);
        this.content = content;
        this.context = context;
        this.mactivity = context;
        this.dialogPayInterface = dialogPayInterface;
        this.sms = sms;
        this.wechat = wechat;
        this.print = print;
        is_pass = is_password;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_jici_consume);
        idImgClose = findViewById(R.id.id_img_close);
        idTvContent = findViewById(R.id.id_tv_content);
        idTvRight = findViewById(R.id.id_tv_right);
        idTvClose = findViewById(R.id.id_tv_close);
        idTvType = findViewById(R.id.id_tv_max_type);
        idTvType.setText("确认扣次");
        idCheckSms = findViewById(R.id.id_check_sms);
        idCheckVx = findViewById(R.id.id_check_vx);
        idCheckPrint = findViewById(R.id.id_check_print);
        idCheckNext = findViewById(R.id.id_check_next);
        editText = findViewById(R.id.id_edt_type_name);
        view = findViewById(R.id.id_view_password);
        initDate();


        idTvContent.setText(content + "");
        initView();
        initclick();
        if (sms) {
            idCheckSms.setAlpha((float) 1);
            idCheckSms.setClickable(true);
        } else {
            idCheckSms.setAlpha((float) 0.3);
            idCheckSms.setClickable(false);
        }
        if (wechat) {
            idCheckVx.setAlpha((float) 1);
            idCheckVx.setClickable(true);
        } else {
            idCheckVx.setAlpha((float) 0.3);
            idCheckVx.setClickable(false);
        }
        if (print) {
            idCheckPrint.setAlpha((float) 1);
            idCheckPrint.setClickable(true);
        } else {
            idCheckPrint.setAlpha((float) 0.3);
            idCheckPrint.setClickable(false);
            idCheckPrint.setChecked(false);//避免切换账号以后打钩残留的问题
        }
        initGetTuisong();
    }


    private void initGetTuisong() {
        HttpRequest.postUrl(Api.GET_SEND_STATUS)
                .addParams("send_view", "Consume")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(getContext(), json);
                        if (checkCode) {
                            Gson gson = new Gson();
                            SendModel sendModel = gson.fromJson(json, SendModel.class);
                            if (sendModel != null) {
                                if ("1".equals(sendModel.getData().getInfo().getSend_sms_status()) && sms) {
                                    idCheckSms.setChecked(true);
                                }
                                if ("1".equals(sendModel.getData().getInfo().getSend_wechat_status()) && wechat) {
                                    idCheckVx.setChecked(true);
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                    }
                });
    }

    private void initView() {
        if (is_pass) {
            view.setVisibility(View.VISIBLE);
            editText.setVisibility(View.VISIBLE);
            idTvContent.setVisibility(View.GONE);
        } else {
            editText.setVisibility(View.GONE);
            view.setVisibility(View.GONE);
            idTvContent.setVisibility(View.VISIBLE);
        }
        boolean aBoolean = PreferencesUtils.getBoolean(getContext(), CommonConstants.CONSUME_DAYING, false);
        idCheckPrint.setChecked(aBoolean);

        Drawable drawable = getContext().getResources().getDrawable(R.drawable.selector_checkbox_style);
        //设置drawable对象的大小
        drawable.setBounds(0, 0, 40, 40);
        Drawable drawable2 = getContext().getResources().getDrawable(R.drawable.selector_checkbox_style);
        //设置drawable对象的大小
        drawable2.setBounds(0, 0, 40, 40);
        Drawable drawable3 = getContext().getResources().getDrawable(R.drawable.selector_checkbox_style);
        //设置drawable对象的大小
        drawable3.setBounds(0, 0, 40, 40);
        Drawable drawable4 = getContext().getResources().getDrawable(R.drawable.selector_checkbox_style);
        //设置drawable对象的大小
        drawable4.setBounds(0, 0, 40, 40);
        idCheckSms.setCompoundDrawables(drawable, null, null, null);
        idCheckVx.setCompoundDrawables(drawable2, null, null, null);
        idCheckPrint.setCompoundDrawables(drawable3, null, null, null);
        idCheckNext.setCompoundDrawables(drawable4, null, null, null);
    }

    public void initclick() {
        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        idTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (is_pass && Utils.isEmpty(editText.getText().toString())) {
                    MyToast.showShortToast(context,"请输入支付密码");
                    return;
                } else {
                    dialogPayInterface.getJiciCheck(idCheckNext.isChecked(), idCheckNext.getText().toString(),editText.getText().toString());
                    dismiss();
                }
            }
        });
        idTvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });


        idCheckPrint.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                PreferencesUtils.putBoolean(getContext(), CommonConstants.CONSUME_DAYING, isChecked);
            }
        });
        idCheckSms.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                saveSendStatus("send_sms", isChecked, idCheckSms);
            }
        });
        idCheckVx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                saveSendStatus("send_wechat", isChecked, idCheckVx);
            }
        });
        idCheckNext.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if ("再次消费提醒".equals(idCheckNext.getText().toString())) {
                        idCheckNext.setChecked(false);
                        initPickerData(idCheckNext);
                    }
                } else {
                    idCheckNext.setText("再次消费提醒");
                }
            }
        });


    }

    private int mYear, mMonth, mDay;

    private void initDate() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mYear = c.get(Calendar.YEAR);// 获取当前年份
        mMonth = c.get(Calendar.MONTH) + 1;// 获取当前月份
        mDay = c.get(Calendar.DAY_OF_MONTH);// 获取当前月份的日期号码
    }

    //时间选择
    private void initPickerData(final CheckBox tvView) {
        final DatePicker picker = new DatePicker(mactivity);
        picker.setCanLoop(false);
        picker.setWheelModeEnable(true);
        picker.setTopPadding(15);
        picker.setRangeStart(2018, 1, 01);
        picker.setRangeEnd(2108, 11, 11);
        picker.setSelectedItem(mYear, mMonth, mDay);
        picker.setWeightEnable(true);
        picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                tvView.setText(year + "-" + month + "-" + day);
                tvView.setChecked(true);

            }
        });
        picker.setOnWheelListener(new DatePicker.OnWheelListener() {
            @Override
            public void onYearWheeled(int index, String year) {
                picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay());
            }

            @Override
            public void onMonthWheeled(int index, String month) {
                picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
            }

            @Override
            public void onDayWheeled(int index, String day) {
                picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
            }
        });
        picker.show();
    }

    private void saveSendStatus(String sendType, boolean status, final CheckBox checkBox) {
        HttpRequest.postUrl(Api.SAVE_SEND_STATUS)
                .addParams("send_status", status ? "1" : "0")
                .addParams("send_type", sendType)
                .addParams("send_view", "Consume")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(context, json);
                        if (!checkCode) {
                            checkBox.setChecked(false);
                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
    }


}
